<? php
//category_edit.tpl:
	{{extends file="admin/admin_header.tpl"}}
	{{block name="content"}}
	<!--根据Method的显示不同的标题内容-->
	<h3>{{if 'Method'=='add'}}添加新栏目{{else}}编辑栏目{{/if}}
	<div class="pull-right">
		<a href="{{site_url('category/index')}}" class="btn btn-sm btn-default">« 返回栏目列表</a>
	</div>
	</h3>
	<ul class="nav nav-tabs">
		<li class="active">
			<a href="#tab-general" style="background-color: #f8f8f8 !important;">栏目信息</a>
		</li>
	</ul>
	<form action="{{site_url('category/store')}}" method="post" class="form-horizontal" autocomplete="off" style="background:#f8f8f8;padding:1em;border:1px solid #ddd;border-top:0;">
		<div class="tab-content">
		<input type="hidden" name="Method" value="{{$Method|h}}">
		<input type="hidden" name="Id" value="{{$category.0.Id}}">
			<div class="tab-pane active" id="tab-general" style="margin: 0 20px;">
				<div class="form-group">
					<label for="name">上级栏目</label>
					<select name="Pid" class="form-control">
						<option value="0" {{if $category[0]['Pid']==0}}selected="selected"{{/if}}>顶级栏目</option>
						<!--排除因错选上级栏目而重新提交表单的情况-->
						{{foreach $lastcate as $v}}
						<option value="{{$v.Id|h}}" {{if $v.Id==$category[0]['Pid']}}selected="selected"{{/if}}{{if $v.Id==$category[0]['Id']}}disabled="disabled"{{/if}}>{{$v.CateName}}</option>
						{{/foreach}}
					</select>
				</div>
				<div class="form-group">
					<label for="name">栏目名称</label>
					<input type="text" name="CateName"  class="form-control" value="{{$category.0.CateName}}">
				</div>
				<div class="form-group">
					<label for="name">栏目类型</label>
					<select name="Type" class="form-control">
						<option value="目录" {{IsSelected('目录',$v.Type)}}>目录</option>
						<option value="文章列表" {{IsSelected('文章列表',$category.0.Type)}}>文章列表</option>
						<option value="教师列表" {{IsSelected('教师列表',$category.0.Type)}}>教师列表</option>
						<option value="下载列表" {{IsSelected('下载列表',$category.0.Type)}}>下载列表</option>
						<option value="链接列表" {{IsSelected('链接列表',$category.0.Type)}}>链接列表</option>
						<option value="单页面" {{IsSelected('单页面',$category.0.Type)}}>单页面</option>
					</select>
				</div>
				<div class="form-group">
					<label for="name">栏目排序</label>
					<input type="text" name="Sort" class="form-control" value="{{$category.0.Sort}}"/>
				</div>
			</div>
		</div>
		<div class="control-group">
			<div class="controls">
				<button type="reset" class="btn btn-default">清空</button>
				<button type="submit" class="btn btn-success">提交</button>
			</div>
		</div>
	</form>
	{{/block}}


//category.php:
	/**
	 * 添加栏目动作
	 */
	function store(){
		$data=array(
			'CateName'=>LfRequest::JudgeRequst('CateName'),
			'Type'=>LfRequest::JudgeRequst('Type'),
			'Sort'=>LfRequest::inNum('Sort'),
			'Pid'=>LfRequest::inNum('Pid'),
			);
		$Method=LfRequest::JudgeRequst('Method');
		if($Method=='add'){
			$res=$this->db->insert('category',$data);
			if($res){
				$_SESSION['info']="栏目添加成功";
				ilog($_SESSION['UserName'].'添加了栏目:'.$data['CateName']);
				redirect('category/index');
			}
		}
		if($Method=='edit'){
			$Id=LfRequest::JudgeRequst('Id');
			// if($Id==$data['Pid']){//由于在tpl中进行了控制，所以不存在上级栏目是本身的情况
			// 	show_error('上级栏目不能是本身');
			// 	return;
			// }
			$res=$this->db->where('Id',$Id)->update('category',$data);
			if($res){
				$_SESSION['info']='栏目修改成功';
				ilog($_SESSION['UserName'].'修改了Id为'.$Id.'的栏目名:'.$data['CateName']);
				redirect('category/index');
			}
		}
	}